package com.briup.jd2511.web02.controller;

import com.briup.jd2511.web02.entity.Student;
import com.briup.jd2511.web02.entity.StudentWithAddress;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@RestController
public class RequestController {

    @RequestMapping("/simpleParam")
    public String simpleParma(@RequestParam(value = "name") String username,
                              @RequestParam(value = "pwd",required = false) String password){
        // 进行判断
        System.out.println(username+":"+password);
        return "success";
    }

    @RequestMapping("/simplePojo")
    public String simplePojo(Student stu){
        // 注意，传递的参数名称要和Student的属性保持一致
        System.out.println(stu);
        return "ok";
    }

    @RequestMapping("/complexPojo")
    public String complexPojo(StudentWithAddress swd){
        System.out.println(swd);
        return "ok";
    }

    @RequestMapping("/arrayParam")
    public String arrayParam(String[] likes){
        System.out.println(Arrays.toString(likes));
        return "ok";
    }

    @RequestMapping("/listParam")
    public String listParam(List<String> likes){
        System.out.println(likes);
        return "ok";
    }

    @RequestMapping("/dateParam")
    public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime dateTime){
        System.out.println("转换后的输出为："+dateTime.toString().replace("T"," "));
        return "ok";
    }

    @RequestMapping(value = "/jsonParam",method = RequestMethod.POST)
    public String jsonParam(@RequestBody StudentWithAddress swd){
        System.out.println(swd);
        return "ok";
    }

    @RequestMapping("/query/{id}")
    public String selectById(@PathVariable String id){
        System.out.println("路径参数id的值："+id);
        return "ok";
    }

    @RequestMapping("/book/{id}/{chapterId}")
    public String select(@PathVariable String chapterId,
                         @PathVariable String id){
        return id+"/"+chapterId;
    }
}

